﻿using Dapper;
using Finance.API.Read.Application.Command.FinancialCategoryCommand;
using Finance.Domain;
using Finance.ErrorCode;
using Finance.Infrastructrue.Implement;
using MediatR;
using System.Text;

namespace Finance.API.Read.Application.Hander.FinancialCategoryHander
{
    public class SelectFinancialCategoryHander:IRequestHandler<SelectFinancialCategoryCommand,APIResult<List<FinancialCategory>>>
    {
        DatabaseContext db;
        ILogger<SelectFinancialCategoryHander> logger;

        public SelectFinancialCategoryHander(DatabaseContext db, ILogger<SelectFinancialCategoryHander> logger)
        {
            this.db = db;
            this.logger = logger;
        }

        public async Task<APIResult<List<FinancialCategory>>> Handle(SelectFinancialCategoryCommand request, CancellationToken cancellationToken)
        {
            APIResult < List<FinancialCategory> > result=new APIResult<List<FinancialCategory>> ();
            StringBuilder builder1 = new StringBuilder ();
            try
            {
                builder1.Append(@"SELECT
                          FinancialCategoryId,
                          CategoryNumber,
                          CategoryName,
                          Remark,
                          IsDel
                        FROM
                          financialCategory
                        WHERE
                          IsDel = false ");
                

                

                var sql = await db.DapperConnection.QueryAsync<FinancialCategory>(builder1.ToString());

                result.Code = APIEnum.Ok;
                result.Message = "下拉框成功";
                result.Data= sql.ToList();
                logger.LogInformation("下拉框成功");
                return result;
            }
            catch (Exception ex)
            {
                result.Code = APIEnum.Ok;
                result.Message = "下拉框错误" + ex.Message;
                throw;
            }
        }
    }
}
